package com.quyunshuo.module.home.activity.time

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.view.WindowManager
import androidx.activity.viewModels
import com.quyunshuo.edwinpro.common.ui.BaseActivity
import com.quyunshuo.module.home.activity.concentration.ConcentrationViewModel
import com.quyunshuo.module.home.databinding.HomeActivityTimeBinding
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar

@AndroidEntryPoint
class TimeActivity :
    BaseActivity<HomeActivityTimeBinding, ConcentrationViewModel>() {

    companion object {
        fun newInstance(context: Context) {
            context.startActivity(Intent(context, TimeActivity::class.java))
        }
    }

    /**
     * 通过 viewModels() + Hilt 获取 ViewModel 实例
     */
    override val mViewModel by viewModels<ConcentrationViewModel>()

    override fun createVB() = HomeActivityTimeBinding.inflate(layoutInflater)

    private var oldNumber: Calendar = Calendar.getInstance()

    // 创建主线程的 Handler
    private val handler = Handler(Looper.getMainLooper())

    // 定义每秒更新时间的 Runnable
    private val updateTimeRunnable = object : Runnable {
        override fun run() {
            updateTime()
            // 每秒执行一次
            handler.postDelayed(this, 1000)
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun HomeActivityTimeBinding.initView() {
        // 屏幕常亮
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        // 初始化时钟显示
        bitFlip1.flip(oldNumber.get(Calendar.HOUR_OF_DAY), 24, TimeTAG.hour)
        bitFlip2.flip(oldNumber.get(Calendar.MINUTE), 60, TimeTAG.min)
        bitFlip3.flip(oldNumber.get(Calendar.SECOND), 60, TimeTAG.sec)

        // 启动时间更新
        handler.post(updateTimeRunnable)
    }

    override fun HomeActivityTimeBinding.initListener() {
        // 可以添加其他事件监听
    }

    /**
     * 更新时间方法
     */
    private fun updateTime() {
        val now = Calendar.getInstance()
        val nhour = now[Calendar.HOUR_OF_DAY]
        val nminute = now[Calendar.MINUTE]
        val nsecond = now[Calendar.SECOND]

        val ohour = oldNumber[Calendar.HOUR_OF_DAY]
        val ominute = oldNumber[Calendar.MINUTE]
        val osecond = oldNumber[Calendar.SECOND]

        oldNumber = now

        val hourDiff = nhour - ohour
        val minuteDiff = nminute - ominute
        val secondDiff = nsecond - osecond

        // 更新小时
        if (hourDiff >= 1 || hourDiff == -23) {
            mBinding.bitFlip1.smoothFlip(1, 24, TimeTAG.hour, false)
        }

        // 更新分钟
        if (minuteDiff >= 1 || minuteDiff == -59) {
            mBinding.bitFlip2.smoothFlip(1, 60, TimeTAG.min, false)
        }

        // 更新秒钟
        if (secondDiff >= 1 || secondDiff == -59) {
            mBinding.bitFlip3.smoothFlip(1, 60, TimeTAG.sec, false)
        }
    }

    override fun initObserve() {
        // 观察 ViewModel 数据变化
    }

    override fun initRequestData() {
        // 初始化数据请求
    }

    override fun onResume() {
        super.onResume()
        // 恢复时间更新
        handler.post(updateTimeRunnable)
    }

    override fun onPause() {
        super.onPause()
        // 暂停时间更新
        handler.removeCallbacks(updateTimeRunnable)
    }

    override fun onDestroy() {
        super.onDestroy()
        // 移除所有回调，防止内存泄漏
        handler.removeCallbacksAndMessages(null)
    }
}